﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DuJianliang.ApplicationPlatform.Flow
{
    public partial class FlowToDo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridView();   
        }

        private void BindGridView()
        {

            string getToDoSQL = @"SELECT A.* FROM dbo.FlowInstance A 
                                    INNER JOIN dbo.FlowInstanceStep B ON A.FlowInstanceID = B.FlowInstanceID AND B.Completed = 0 AND B.StepID IN 
                                    (SELECT StepID FROM dbo.FlowStepOperator WHERE OperatorInfo = @OperatorInfo)
                                    WHERE A.Completed = 0 ORDER BY A.CreateDate DESC";

            string getOperatorSQL = @"SELECT TOP 1 OperatorInfo FROM dbo.FlowInstanceStep WHERE StepID =
                                        (SELECT TOP 1 StepID FROM dbo.FlowInstanceStep
                                        WHERE FlowInstanceID = @FlowInstanceID
                                        ORDER BY StepNumber DESC) AND FlowInstanceID = @FlowInstanceID";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(getToDoSQL);
            database.AddInParameter(dbCommand, "@OperatorInfo", DbType.String, Common.CurrentInfo.Instance.CurrentUser.UserName);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            DataTable dataSource = dataTable.Clone();
            
            foreach (DataRow dataRow in dataTable.Rows)
            {
                string flowInstanceID = dataRow["FlowInstanceID"] != null ? dataRow["FlowInstanceID"].ToString() : string.Empty;
                string operationID = dataRow["OperationID"] != null ? dataRow["OperationID"].ToString() : string.Empty;

                // 如果是驳回的流程，则首先找出上一次驳回步骤的处理人，如果处理人是当前用户则显示。
                if (operationID.Equals("4"))
                {
                    dbCommand = database.GetSqlStringCommand(getOperatorSQL);
                    database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
                    object obj = database.ExecuteScalar(dbCommand);
                    if (obj != null 
                        && obj.ToString().ToLower().Equals(Common.CurrentInfo.Instance.CurrentUser.UserName.ToLower()))
                    {
                        dataSource.Rows.Add(dataRow.ItemArray);
                    }
                }
                else
                {
                    dataSource.Rows.Add(dataRow.ItemArray);
                }
            }

            gvToDoList.DataSource = dataTable;
            gvToDoList.DataBind();
        }

        protected void gvToDoList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button button = e.Row.Cells[9].FindControl("btnOpen") as Button;

                if (button != null)
                {                    
                    string flowID = gvToDoList.DataKeys[e.Row.RowIndex]["FlowID"].ToString();
                    string flowInstanceID = gvToDoList.DataKeys[e.Row.RowIndex]["FlowInstanceID"].ToString();
                    string url = string.Format("FlowManager.aspx?flowID={0}&FlowInstanceID={1}", flowID, flowInstanceID);

                    button.Attributes.Add("onclick", string.Format("showPage('{0}');", url));
                }
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindGridView();
        }        
    }
}